package util

import "strings"

func Distinct[T comparable](src []T) []T {
	dst := []T{}
	m := map[T]bool{}
	for _, i := range src {
		if m[i] {
			continue
		}
		m[i] = true
		dst = append(dst, i)
	}
	return dst
}

func ToMap[T comparable](list []T) map[T]bool {
	m := map[T]bool{}
	for _, s := range list {
		m[s] = true
	}
	return m
}

func ToList[T comparable](m map[T]bool) []T {
	list := []T{}
	for s := range m {
		list = append(list, s)
	}
	return list
}

func Contains[T comparable](list []T, elem T) bool {
	for _, r := range list {
		if r == elem {
			return true
		}
	}
	return false
}

func PadLeft(s string, n int, c byte) string {
	l := len(s)
	if l == n {
		return s
	}
	if l > n {
		return s[:n]
	}
	return strings.Repeat(string(c), n-l) + s
}

func PadRight(s string, n int, c byte) string {
	l := len(s)
	if l == n {
		return s
	}
	if l > n {
		return s[:n]
	}
	return s + strings.Repeat(string(c), n-l)
}

func IsAlphaNum(s string) bool {
	for _, c := range []byte(s) {
		if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
			return false
		}
	}
	return true
}
